package lex.dm.ui;

import java.awt.BorderLayout;
import java.sql.ResultSet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.lex.swing.ContainerFactory;

import lex.dm.ui.cmd.CommandPane;
import lex.dm.ui.dbtree.DatabaseTree;
import lex.dm.ui.results.ResultsPane;

public class MainContentPane extends JPanel {
	private JSplitPane mainSplitPane = null;
	private DatabaseTree databaseTree = null;
	private JSplitPane rightSplitPane = null;
	private ResultsPane resultsPane = null;
	private CommandPane commandPane = null;

	public MainContentPane() {
		super(new BorderLayout(0, 0));
		this.add(new MainToolBar(), BorderLayout.NORTH);
		// -----------------------------------------------
		rightSplitPane = ContainerFactory.createBorderlessSplitPane(JSplitPane.VERTICAL_SPLIT, 5);
		rightSplitPane.setName(getClass().getName() + ".rightSplit");
		rightSplitPane.setResizeWeight(0.4D);
		rightSplitPane.setTopComponent(this.getCommandPane());
		rightSplitPane.setBottomComponent(this.getResultsPane());
		// -------------------------------------------------------
		mainSplitPane = ContainerFactory.createBorderlessSplitPane(JSplitPane.HORIZONTAL_SPLIT, 5);
		mainSplitPane.setName(getClass().getName() + ".mainSplit");
		mainSplitPane.setResizeWeight(0.3D);
		mainSplitPane.setLeftComponent(new JScrollPane(getDatabaseTree()));
		mainSplitPane.setRightComponent(rightSplitPane);
		this.add(mainSplitPane, BorderLayout.CENTER);
	}

	public void setConnected(boolean b) {
		this.getDatabaseTree().setConnected(b);
	}

	public void execute(String sql) {
		this.getResultsPane().execute(sql);
	}

	public void addResultSet(ResultSet rs) {
		this.getResultsPane().addResultSet(rs);
	}

	public void addResultSet(String title, ResultSet rs) {
		this.getResultsPane().addResultSet(title, rs);
	}

	private DatabaseTree getDatabaseTree() {
		if (null == databaseTree)
			databaseTree = new DatabaseTree();
		return databaseTree;
	}

	private ResultsPane getResultsPane() {
		if (null == resultsPane)
			resultsPane = new ResultsPane();
		return resultsPane;
	}

	private CommandPane getCommandPane() {
		if (null == commandPane)
			commandPane = new CommandPane();
		return commandPane;
	}
}
